#include <ctype.h>
#include <stdarg.h>
#include <stdint.h>
#include "port/datatype.h"
#include "onps_errors.h"
#include "port/sys_config.h"
#include "port/os_datatype.h"
#include "one_shot_timer.h"
#include "sys_timer.h"
#include "onps_utils.h"
#include "protocols.h"
#include "onps_input.h"
#include "ip/tcp.h"

#define SYMBOL_GLOBALS
#include "port/os_adapter.h"
#undef SYMBOL_GLOBALS

#if SUPPORT_ETHERNET
const CHAR *or_pszaEthName[ETHERNET_NUM] = {
    "eth0"
};
#endif

//* 协议栈内部工作线程列表
#define THOSTIMERCOUNT_STK_SIZE	  256 * 4
#define THOSTIMERCOUNT_TIMESLICE  10

#if SUPPORT_SACK
#define THTCPHANDLER_STK_SIZE	  256 * 4
#define THTCPHANDLER_TIMESLICE  5
#endif
const static STCB_PSTACKTHREAD lr_stcbaPStackThread[] = {
	{ thread_one_shot_timer_count, (void *)0, "OSTimerCnt", THOSTIMERCOUNT_STK_SIZE, THOSTIMERCOUNT_PRIO, THOSTIMERCOUNT_TIMESLICE }, 
  
#if SUPPORT_SACK
  { thread_tcp_handler, (void *)0, "TcpHandler", THTCPHANDLER_STK_SIZE, THTCPHANDLER_PRIO, THTCPHANDLER_TIMESLICE },
#endif  
}; 


//* 当前线程休眠指定的秒数，参数unSecs指定要休眠的秒数
void os_sleep_secs(UINT unSecs)
{
	rt_thread_mdelay(unSecs * 1000); 
}

//* 当前线程休眠指定的毫秒数，单位：毫秒 
void os_sleep_ms(UINT unMSecs)
{
    rt_thread_mdelay(unMSecs); 
}

//* 获取系统启动以来已运行的秒数（从0开始）
UINT os_get_system_secs(void)
{
	return GetElapsedSecs(); 
}

//* 获取系统启动以来已运行的毫秒数（从0开始）
UINT os_get_system_msecs(void)
{
    return GetElapsedMSecs();
}

void os_thread_onpstack_start(void *pvParam)
{
	pvParam = pvParam; //* 避免编译器警告

	//* 建立工作线程
  rt_thread_t tid; 
	INT i; 
	for (i = 0; i < sizeof(lr_stcbaPStackThread) / sizeof(STCB_PSTACKTHREAD); i++)
	{
		tid = rt_thread_create(lr_stcbaPStackThread[i].pszThreadName, lr_stcbaPStackThread[i].pfunThread, RT_NULL, lr_stcbaPStackThread[i].unStackSize, lr_stcbaPStackThread[i].ubPrio, lr_stcbaPStackThread[i].unTimeSlice);  
    if(tid != RT_NULL)
      rt_thread_startup(tid); 
	}
}

HMUTEX os_thread_mutex_init(void)
{
	HMUTEX hMutex = rt_mutex_create("rt-mutex", RT_IPC_FLAG_FIFO);
  if(RT_NULL != hMutex)      
    return hMutex; 
    
	return INVALID_HMUTEX; //* 初始失败要返回一个无效句柄
}

void os_thread_mutex_lock(HMUTEX hMutex)
{
	rt_mutex_take(hMutex, RT_WAITING_FOREVER); 
}

void os_thread_mutex_unlock(HMUTEX hMutex)
{
	rt_mutex_release(hMutex);
}

void os_thread_mutex_uninit(HMUTEX hMutex)
{
	rt_mutex_delete(hMutex); 
}

HSEM os_thread_sem_init(UINT unInitVal, UINT unCount)
{
  unCount = unCount; //* 避免编译器警告
  
	HSEM hSem = rt_sem_create("rt-dsem", unInitVal, RT_IPC_FLAG_FIFO); 
  if(RT_NULL != hSem)
    return hSem; 

	return INVALID_HSEM; //* 初始失败要返回一个无效句柄
}

void os_thread_sem_post(HSEM hSem)
{
	rt_sem_release(hSem); 
}

INT os_thread_sem_pend(HSEM hSem, INT nWaitSecs)
{    
  if(nWaitSecs)
    nWaitSecs = nWaitSecs * RT_TICK_PER_SECOND; 
  else
    nWaitSecs = RT_WAITING_FOREVER; 
  
	rt_err_t lRtnVal = rt_sem_take(hSem, nWaitSecs);
  if(RT_EOK == lRtnVal)
    return 0; 
  else if(-RT_ETIMEOUT == lRtnVal)
    return 1; 
  else    
    return -1; 
}

void os_thread_sem_uninit(HSEM hSem)
{
	rt_sem_delete(hSem);
}
